﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using NodeBasedSkillEditor.ViewModel;

namespace NodeBasedSkillEditor.View
{
    public class PortControl : UserControl
    {
        public string PortName {
            get { return (string)GetValue(PortNameProperty); }
            set { SetValue(PortNameProperty, value); }
        }

        public static readonly DependencyProperty PortNameProperty =
            DependencyProperty.Register("PortName", typeof(string), typeof(PortControl), new PropertyMetadata(""));

        SolidColorBrush PortColor { get; } = new SolidColorBrush(Colors.Black);
        public void ChangePortColor(Color color)
        {
            PortColor.Color = color;
            InvalidateVisual();
        }

        Pen _portStrockPen { get; } = new Pen(new SolidColorBrush(Color.FromRgb(228, 228, 228)), 1);
        SolidColorBrush _expandColor { get; }
        Pen _expandPen { get; }

        VM_Port _vm { get { return DataContext as VM_Port; } }

        public virtual UIElement ShowingPortUIE { get; }
        public virtual Point ShapeOffset { get; }
        public Vector OffsetAboutParentNode { get; private set; }
        public Point Position { get { return _vm.ParentNode.Position + OffsetAboutParentNode; } }

        public virtual Point DrawPortOffset { get; }

        public PortControl()
        {
            _portStrockPen.Freeze();
            _expandColor = new SolidColorBrush(Colors.White);
            _expandColor.Freeze();
            _expandPen = new Pen(_expandColor, 1.5);
            _expandPen.Freeze();
        }

        public void UpdateOffsetAboutParentNode()
        {
            OffsetAboutParentNode = (Vector)ShowingPortUIE.TranslatePoint(ShapeOffset, _vm.ParentNode.BindingNodeCtrl);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            DrawPort(drawingContext);
        }

        void DrawPort(DrawingContext dc)
        {
            if (_vm == null)
            {
                dc.DrawEllipse(PortColor, _portStrockPen, DrawPortOffset, 5, 5);
                return;
            }

            if (_vm.IsListPort)
            {
                var rect = new Rect(new Size(10, 10));
                rect.SetCenter(DrawPortOffset);
                dc.DrawRectangle(PortColor, _portStrockPen, rect);
            }
            else
            {
                dc.DrawEllipse(PortColor, _portStrockPen, DrawPortOffset, 5, 5);
            }

            if (_vm.IsNormalPortInGroup)
            {
                var rect = new Rect(new Size(4, 4));
                rect.SetCenter(DrawPortOffset);
                dc.DrawRectangle(_expandColor, null, rect);
            }
            else if (_vm.IsExpandPort)
            {
                var rect = new Rect(new Size(4, 4));
                rect.SetCenter(DrawPortOffset);
                dc.DrawRectangle(null, _expandPen, rect);
            }
        }
    }
}
